package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.mapper.CustomerTraceHistoryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTraceHistoryServiceImpl implements ICustomerTraceHistoryService {
    @Autowired
    private CustomerTraceHistoryMapper customerTraceHistoryMapper;

    public PageInfo<CustomerTraceHistory> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<CustomerTraceHistory> list = customerTraceHistoryMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(CustomerTraceHistory entity) {
        //自动填入录入时间
        entity.setInputTime(new Date());
        if (entity.getId() == null) {
            //自动填入录入人
            entity.setInputUser(UserContext.getCurrentUser());
            customerTraceHistoryMapper.insert(entity);
        } else {
            customerTraceHistoryMapper.updateByPrimaryKey(entity);
        }
    }
}
